package org.hb0712.sya.tools.database.common;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.ObjectMapper;

public class MyConnection {
    private Connection connection;
    private String jdbcUrl;
    private String username;
    private String password;
    private String hostname;
    private String publicKeyStr;
    private String privateKeyStr;
    // 加密算法名称
    public static final String RSA_ALGORITHM = "RSA";
    
    public void connect() {
        /*
        try {
            connection = DriverManager.getConnection(jdbcUrl, username, password);
        } catch (SQLException e) {
            System.err.println("数据库连接失败，错误信息:");
            e.printStackTrace();
        }
        */
    }
    
    public void close() {
        /*
        try {
            connection.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
        */
    }
    
    public List<Map<String, Object>> queryForList(String sql) {
        List<Map<String, Object>> list = null;
        try {
            list = excute(sql);
        } catch (Exception e) {
        /*
            list = excute(sql, connection);
        } catch (SQLException e) {
        */
            e.printStackTrace();
        }
        return list;
    }
    
    /*
     * 发送HTTP请求，通过RSA方式加密请求内容，以POST方法请求API
     */
    private List<Map<String, Object>> excute(String sql) throws Exception {
        // 注意，sql的长度不能大于245
        String encryptedData = RSAUtils.encrypt(sql, publicKeyStr);
        // 加密后的数据
        String jsonData = "{\"encryptedDataBase64\":\""+encryptedData+"\"}";
        // 请求头设置
        Map<String, String> headers = Map.of(
            "Content-Type", "application/json",
            "Authorization", "Bearer your_token_here",
            "User-Agent", "Java HttpClient"
        );
        
        String response = HttpUtils.sendPostRequestSync(hostname, jsonData, headers);
        // 字符串转换为对象
        ObjectMapper objectMapper = new ObjectMapper();
        ActionResult actionResult = objectMapper.readValue(response, ActionResult.class);
        return actionResult.getData();
    }
    
    private List<Map<String, Object>> excute(String sql, Connection connection) throws SQLException {
        List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
        
        Statement statement = connection.createStatement();
        // 执行SQL查询
        ResultSet resultSet = statement.executeQuery(sql);
        ResultSetMetaData rsmd = resultSet.getMetaData();
        
        int columnCount = rsmd.getColumnCount();
        // 处理结果集
        while (resultSet.next()) {
            Map<String, Object> map = new LinkedHashMap<String, Object>();
            for (int i=1;i<=columnCount;i++) {
                String key = rsmd.getColumnName(i);
                Object value = resultSet.getObject(i);
                map.put(key, value);
            }
            list.add(map);
        }
        return list;
    }
    
    public void setJdbcUrl(String jdbcUrl) {
        this.jdbcUrl = jdbcUrl;
    }
    
    public void setUsername(String username) {
        this.username = username;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public void setHostname(String hostname) {
        this.hostname = hostname;
    }
    
    public void setPublicKeyStr(String publicKeyStr) {
        this.publicKeyStr = publicKeyStr;
    }
    
    public void setPrivateKeyStr(String privateKeyStr) {
        this.privateKeyStr = privateKeyStr;
    }
    
    
}
